package com.fact.actions;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.fact.dto.RoleDTO;
import com.fact.dto.ScenarioDTO;
import com.fact.models.RoleService;
import com.fact.utils.AppConstant;

public class Role extends BaseAction implements SessionAware{
	
	private static final long serialVersionUID = 1L;
	private Map<String,Object> session;
	private ArrayList<RoleDTO> roleList;
	
	private String page=AppConstant.ROLE;
	private String pageTitle="FMtool - Role List";

	RoleDTO role=new RoleDTO();
	private String opType;
	RoleService roleService=new RoleService();
	
	public String roleList()
	{
		if(opType==null || (!opType.equalsIgnoreCase("save") && !opType.equalsIgnoreCase("delete") && !opType.equalsIgnoreCase("edit")))
			this.session.put("operation_message", null);
		
		roleList= roleService.getRoleList();
		
		return SUCCESS;
	}
	public String newRoleForm()
	{		
		return SUCCESS;
	}
	public String saveRole()
	{ 
		
		if(roleService.getRole(role.getRoleName())!=null)
			addActionMessage("Role Already Exist.");
		
		if(hasActionMessages())
			return  INPUT;


		if(roleService.saveRole(role)==false)
				{
					addActionMessage("Error in Role Save Operation.");
					return INPUT;
				}
		else
				{
					this.session.put("operation_message", role.getRoleName()+" has successfully been added in Role List.");
					return  SUCCESS;
				}

	}

	public String editRole()
	{
		this.session.put("operation_message", null);
		RoleDTO roleTmp=roleService.getRole(role.getRoleId());
		boolean editFlag=false;
		if(roleTmp==null)
		{
			editFlag=true;
		}
		else
		{
			if(roleTmp.getRoleId()!=role.getRoleId())
			{
				editFlag=false;
			}
			else
			{
				editFlag=true;
			}
		}
		if(editFlag==true)
		{
			if(roleService.editRole(role)==false)
			{
				addActionMessage("Error in Role Edit Operation.");
				return INPUT;
				
			}
			else
			{
				this.session.put("operation_message","Role with Role Name '"+ role.getRoleName()+"' has successfully been updated.");
				return  SUCCESS;
			}
		}
		else
		{

			addActionMessage("Update Falied. "+role.getRoleName()+" is already in the System.");
			return INPUT;
		}
	
	}
	public String deleteRole()
	{
		if(roleService.deleteRole(role.getRoleId())==false)
		{
			addActionMessage("Error in Role Delete Operation.");
			return INPUT;
			
		}
		else
		{
			this.session.put("operation_message","Role with Role Name '"+ role.getRoleName()+"' has successfully been deleted from Role List.");
			return  SUCCESS;
		}
	
	}
	public ArrayList<RoleDTO> getRoleList() {
		return roleList;
	}

	public void setRoleList(ArrayList<RoleDTO> roleList) {
		this.roleList = roleList;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getPageTitle() {
		return pageTitle;
	}

	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}

	public RoleDTO getRole() {
		return role;
	}

	public void setRole(RoleDTO role) {
		this.role = role;
	}

	public String getOpType() {
		return opType;
	}

	public void setOpType(String opType) {
		this.opType = opType;
	}

	public void setSession(Map<String, Object> session) 
	{
		this.session = session;
	}
	public ServletContext getServletContext()
	{
		return ServletActionContext.getServletContext();
	}
}
